package com.xg.controller.search;


import com.xg.common.constant.TokenConstant;
import com.xg.common.response.TokenResponse;
import com.xg.common.util.Func;
import com.xg.common.util.JWTUtils;
import com.xg.common.util.PageUtil;
import com.xg.common.util.PageUtilHelper;
import com.xg.controller.dto.UsedCarDto;
import com.xg.entity.UsedCar;
import com.xg.entity.UsedCarBrand;
import com.xg.entity.UsedCarSeries;
import com.xg.entity.UsedCarType;
import com.xg.service.IUsedCarBrandService;
import com.xg.service.IUsedCarSeriesService;
import com.xg.service.IUsedCarService;
import com.xg.service.IUsedCarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/buy")
public class CarSrearchController {

    @Autowired
    private IUsedCarService usedCarService;

    @Autowired
    private IUsedCarBrandService usedCarBrandService;

    @Autowired
    private IUsedCarSeriesService seriesService;

    @Autowired
    private IUsedCarTypeService typeService;

    @RequestMapping("/userLogout")
    public void userLogout(HttpServletRequest request, HttpServletResponse response){
        // 清空token
        Cookie successCookie = null;
        Cookie refreshCookie = null;
        Cookie[] cookies = request.getCookies();
        if (!Func.isEmpty(cookies)){
            for (Cookie cookie : cookies) {
                String successTokenKey = TokenConstant.SUCCESS_TOKEN;
                if (null != cookie && successTokenKey.equals(cookie.getName())){
                    successCookie = cookie;
                    break;
                }
            }
            for (Cookie cookie : cookies) {
                String refreshTokenKey = TokenConstant.REFRESH_TOKEN;
                if (null != cookie && refreshTokenKey.equals(cookie.getName())){
                    refreshCookie = cookie;
                    break;
                }
            }
        }
        successCookie.setMaxAge(0);
        successCookie.setPath("/");
        refreshCookie.setMaxAge(0);
        refreshCookie.setDomain("localhost");
        refreshCookie.setPath("/");
        response.addCookie(successCookie);
        response.addCookie(refreshCookie);
        try {
            response.sendRedirect("http://localhost:9003/user/toLogin");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转搜索页
     * @param model
     * @param brandId
     * @param priceId
     * @param seriesId
     * @param request
     * @return
     */
    @RequestMapping("/searchHome")
    public String search(Model model, Integer brandId, Integer priceId, Integer seriesId, HttpServletRequest request) {
        // 获取token
        String token = "";
        Cookie[] cookies = request.getCookies();
        if (!Func.isEmpty(cookies)){
            for (Cookie cookie : cookies) {
                String successTokenKey = TokenConstant.SUCCESS_TOKEN;
                if (null != cookie && successTokenKey.equals(cookie.getName())){
                    token = cookie.getValue();
                    break;
                }
            }
        }
        if (!Func.isEmpty(token)){
            //jwt解密
            TokenResponse tokenResponse = JWTUtils.checkToken(token);
            System.out.println(tokenResponse.getUsername());
            System.out.println(tokenResponse.getUserPhone());
            //返回用户信息
            model.addAttribute("userName",tokenResponse.getUsername());
            model.addAttribute("userPhone",tokenResponse.getUserPhone());
        }
        //查询基本信息展示的列表
        List<UsedCarBrand> brandList = usedCarBrandService.list();
        List<UsedCarSeries> seriesList = seriesService.list();
        List<UsedCarType> typeList = typeService.list();
        model.addAttribute("brandList",brandList);
        model.addAttribute("seriesList",seriesList);
        model.addAttribute("typeList",typeList);
        //判断从主页传来的对象添加到搜索页
        if (!Func.isEmpty(brandId)){
            model.addAttribute("transmitBrandId",brandId);
        }else {
            model.addAttribute("transmitBrandId",0);
        }
        if (!Func.isEmpty(priceId)){
            model.addAttribute("transmitPriceId",priceId);
        }else {
            model.addAttribute("transmitPriceId",0);
        }
        if (!Func.isEmpty(seriesId)){
            model.addAttribute("transmitSeriesId",seriesId);
        }else {
            model.addAttribute("transmitSeriesId",0);
        }
        return "PurchaseCarNew";
    }

    /**
     * 返回搜索列表分页
     * @param pageUtil
     * @param usedCarDto
     * @param model
     * @return
     */
    @RequestMapping("/buySearchUsedCar")
    public String buySearchUsedCar(PageUtil<UsedCar> pageUtil, UsedCarDto usedCarDto, Model model) {
        System.out.println("pageUtil = " + pageUtil);
        List<UsedCar> list = usedCarService.buySearchUsedCar(usedCarDto);
        PageUtil pageInfo = PageUtilHelper.pageHelper(list, pageUtil.getCpage(),12);
        model.addAttribute("pageInfo",pageInfo);
        return "listPurchaseCarNew";
    }
}
